<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\paginator;
use app\admin\model\UserInfo;
use app\admin\model\Cuser as Cusers;

/**
 *
 * @author sj
 * 前台会员登录管理控制器
 */
class Cuser extends Common
{
    private $cModel;

    public function _initialize()
    {
        parent::_initialize();
        $this->cModel = new Cusers();
    }

    public function index()
    {
        $where = ['type', 'eq', '0'];
        $dataList = Db::name('cuser')->alias('c')->join('cuser_info i', 'c.id=i.uid')->where('c.type', 'eq', 0)->paginate(9);

        $this->assign('dataList', $dataList);
        return $this->fetch();
    }
    public function details($id)
    {
        if (request()->isPost()) {
            $data = input('post.');
            if($data['actions'] == 'infos'){
                unset($data['actions']);
                $result = Db::name('cuser')->alias('c')->join('gx_cuser_info i','c.id = i.uid')->where(['c.id'=>$data['id']])->update($data);

            }elseif($data['actions']=='password'){
                $oldData = $this->cModel->where(['id'=>$data['id'],'password'=>md5($data['repassword'])])->find();
                if(empty($oldData)){
                    return ajaxReturn(lang('oldpassword_val'));
                }
                $result = $this->cModel->validate(CONTROLLER_NAME.'.password')->allowField(true)->save($data,$data['id']);

            }else{

            }


            if ($result) {
                return ajaxReturn(lang('action_success'), url('index'));
            } else {
                return ajaxReturn($this->cModel->getError());
            }


        }else{
            $id = input('id');
            if ($id > 0) {
                $where = ['id' => $id];
                $data = Db::name('cuser')->alias('c')->field()->join('cuser_info i', 'c.id=i.uid')->where(['c.id' => $id])->find();
                $this->assign('data', $data);
                return $this->fetch();
            }
        }

    }


    public function add()
    {

        if (request()->isPost()) {
            $data = input('post.');

        } else {
            # code...
        }


        if ($res) {
            ajaxReturn(lang('action_success', url('csuer/index')));
        } else {

        }

        $data = array('telphone' => '18601649376', 'password' => md5('sjlovefxy'), 'login_count' => 'login_count' + 1);
        $res = $this->cModel->add($data);
        if ($res) {
            ajaxReturn2(1, 'insert sucess', null);
        } else {
            ajaxReturn2(0, 'insert failed', $data);
        }
    }

    public function delete()
    {

        if(\request()->isPost()){
            $id = input('id');

            if (isset($id) && !empty($id)) {
                $id_arr = explode(',', $id);
                $where = ['id'=>['in', $id_arr]];
                $result = $this->cModel->where($where)->delete();

                if ($result) {
                    return ajaxReturn(lang('action_success'), url('cuser/indexCuser'));
                } else {
                    return ajaxReturn($this->cModel->getError());
                }

            }
        }



    }

    public function edit($id)
    {
        if (request()->isPost()){
                $data = input('post.');
                if($data['actions'] == 'password'){
                    $oldData = $this->cModel->where(['id'=>$data['id'],'password'=>md5($data['oldpassword'])])->find();
                    if(empty($oldData)){
                        return ajaxReturn(lang('oldpassword_val'));
                    }

                    $result = $this->cModel->validate(CONTROLLER_NAME.'.password')->allowField(true)->save($data,$data['id']);

                }elseif ($data['actions'] == 'cuser'){

                }else {

                }
                    if($result){
                        return ajaxReturn(lang('action_success'), url('indexCuser'));
                    }else{
                        return ajaxReturn($this->cModel->getError());
                    }

        }else{
            if ($id > 0){
                $data = $this->cModel->get($id);
                $data->userInfo;   //用户附加信息数据
                $this->assign('data', $data);
                if($data['type'] == 1){
                    return $this->fetch('editCuser');
                }else{
                    return $this->fetch();
                }
            }
        }
    }

    public function editWhere()
    {
        $post_data = input('post.');
        //验证数据的验证器
        $res = $this->cModel->CupdateWhere($post_data);
        if ($res) {
            ajaxReturn2(1, 'update sucess', '');
        } else {
            ajaxReturn2(0, 'edit failed', $post_data);
        }

    }

    public function selectLists()
    {
        //分页查询列表
        $page = empty(input('get.page')) ? 1 : input('get.page');
        $limit = empty(input('get.limit')) ? 10 : input('get.limit');
        $res = $this->cModel->CselectLists($page, $limit);

        $total = count($res);
        if ($res) {
            $result['status'] = 1;
            $result['msg'] = 'select sucess';
            $result['data'] = $res;
            $result['total'] = $total;
            return json_encode($result);
        } else {
            ajaxReturn2(0, 'select failed', null);
        }
    }

    public function getItem()
    {
        $id = empty(input('get.id')) ? ajaxReturn2(-1, 'id is miss', '') : input('get.id');
        $where = array('id' => $id);
        $page = empty(input('get.page')) ? 1 : input('get.page');
        $limit = empty(input('get.limit')) ? 10 : input('get.limit');
        $res = $this->cModel->CseletItem($where, $page, $limit);
        if ($res) {
            ajaxReturn2(1, 'select sucess', $res);
        } else {
            ajaxReturn2(0, 'select failed', $where);
        }
    }

    public function deleteAll()
    {
        //默认是软删除
        $ids = empty(input('get.ids')) ? ajaxReturn2(-1, 'ids is miss', '') : input('get.ids');
        $ids = explode(',', $ids);
        $flag = true;
        $res = $this->cModel->Cdelete($ids, $flag);
        if ($res) {
            ajaxReturn2(1, 'deleteAll sucess', '');
        } else {
            ajaxReturn2(0, 'delete failed', $ids);
        }
    }
    //软删除上面修改了一下默认软删除
    //根据业务需求进行真的删除或者软删除默认软删除
    public function deleteSoft()
    {

    }

    //这里的列表显示使用 layui-table 编辑 删除 添加使用表单 可以使用弹出窗
    public function selectV()
    {
        return view('list');
    }


    //商家会员操作
    public function indexCuser()
    {
        $where = [];
        //根据字段条件搜索
        if (input('get.search')) {
            $where['telphone'] = ['like', '%' . input('get.search') . '%'];
        }
        $order = ['id desc'];
        $dataList = $this->cModel->where($where)->where('type', 'eq', '1')->order($order)->paginate('5', false, page_param());
        $this->assign('dataList', $dataList);
        return $this->fetch('indexCuser');
    }

    public function addCuser()
    {
        if(\request()->isPost()){

            $data = input('post.');
            $result =$this->cModel->validate(CONTROLLER_NAME.'.addCuser')->allowField(true)->save($data);
            if($result){
                ajaxReturn(lang('action_success'), url('cuser/indexCuser'));
            }else{
                ajaxReturn($this->cModel->getError());
            }
        }else{
            return $this->fetch('addCuser');
        }


    }

    /******根据Cuser用户的ID 来查询该用户下所有的订单******/
    public function cuserOrderList()
    {
        $id = input('id');
        $where = ['user_id',['in',$id]];
        $dataList = Db::name('order')->alias('o')->join('gx_pay_info p','o.user_id = p.user_id')->where(['o.user_id'=>$id])->paginate('',false,page_param());

        $this->assign('dataList', $dataList);
        return $this->fetch('cuser_order_list');
    }

}